<script setup lang="ts">
import tabCom from "@/components/table/indexcopy.vue"
import { ref, reactive } from "vue"
import { useUserStoreHook } from "@/store/modules/user"
import { changeUserInfo, searchOneUserInfo } from "@/api/common"
import type { FormInstance, FormRules } from "element-plus"

const tabCombox = ref<typeof tabCom>()
let showUser = ref("") // 查看那个用户的
let userThisNmae = ref("") // 当前用户名字
const userInfo = useUserStoreHook()

//#region  用户信息 模块

interface RuleForm {
  name: string
  image: string
  userName: string
  password: string
  gender: number
}

const formSize = ref("default")
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: "",
  image: "",
  userName: "",
  password: "",
  gender: 1
})

ruleForm.name = userInfo.username
ruleForm.image = userInfo.headImg
ruleForm.userName = userInfo.account
ruleForm.gender = userInfo.gender

console.log("userInfo---", userInfo)

const rules = reactive<FormRules<RuleForm>>({
  // name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  // image: [
  //   {
  //     required: true,
  //     message: "请上传头像",
  //     trigger: "change"
  //   }
  // ],
  userName: [
    {
      required: true,
      message: "登录账号不能为空",
      trigger: "blur"
    }
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log("submit!")
      changeUserInfo(ruleForm)
        .then((res) => {
          let userinfos = JSON.parse(localStorage.getItem("userinfo") || "{}")
          userinfos = {
            ...userinfos,
            ...ruleForm
          }
          localStorage.setItem("userinfo", JSON.stringify(userinfos))
          userInfo.getInfo()
          ElMessage({
            message: "修改成功~",
            type: "success"
          })
        })
        .catch(() => {})
    } else {
      console.log("error submit!", fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`
}))

//#endregion 用户信息模块end

//#region  设置用户
let dialogVisible = ref(false) //
let tanOBJUser = ref({
  // 弹框
  userid: "",
  name: ""
})
const userInfoData = ref<any>({}) // 用户信息
// 确认用户
const handleCreate = () => {
  searchOneUserInfo(tanOBJUser.value.userid).then((res: any) => {
    if (res.code == 200) {
      userThisNmae.value = tanOBJUser.value.name
      showUser.value = tanOBJUser.value.userid
      userInfoData.value = res.data
      clearTan()
      handleClick()
    }
  })
}
// 设置用户弹框关闭
const clearTan = () => {
  dialogVisible.value = false
  tanOBJUser.value.name = ""
  tanOBJUser.value.userid = ""
}

//#endregion

//#region 查看用户其他信息

let activeName = ref("first")

const tableConfig = {
  first: {
    api: [{ type: "search", interface: "/userViolationPenalties/selectAll" }],
    table: [
      // { prop: "id", label: "id" },
      { prop: "violationUidName", label: "违规人员" },
      { prop: "violationTime", label: "违规时间" },
      { prop: "violationContent", label: "违规材料", type: "filename", assoc: "violationMaterial" },
      { prop: "enforceUidName", label: "执法人员" },
      {
        prop: "violationState",
        label: "违规状态",
        type: "select",
        list: [
          { value: 0, label: "待审核", name: "待审核", id: 0 },
          { value: 1, label: "已通过", name: "已通过", id: 1 },
          { value: 2, label: "未通过", name: "未通过", id: 2 }
        ]
      },
      { prop: "remarks", label: "备注" }
    ]
  },
  second: {
    api: [{ type: "search", interface: "/userStudyRecord/selectAll" }],
    table: [
      // { prop: "id", label: "id" },
      { prop: "studyName", label: "学习名称" },
      // { prop: "abnormalName", label: "违规员工" },
      {
        prop: "studyMaterialName",
        label: "学习资料",
        type: "filename",
        assoc: "studyMaterialUrl"
      },
      {
        prop: "studyState",
        label: "学习状态",
        type: "select",
        list: [
          { value: "1", label: "未学习", name: "未学习", id: "1" },
          { value: "2", label: "已学习", name: "已学习", id: "2" }
        ]
      },
      { prop: "studyUidName", label: "学习人员" },
      { prop: "studyTime", label: "学习时间", type: "time" },
      { prop: "remarks", label: "备注" }
    ]
  }
}

// 接口请求配置
const operationApiA = ref<any>([])

const mustSearchData = ref<any>({})

// 搜索配置
const searchDataListJy = []

// console.log('searchDataListJy',searchDataListJy.length)

// 添加配置
const searchDataListJyEdit = []
// 表单配置
const tableDataRuleA = ref<any>([])
const handleClick = () => {
  setTimeout(() => {
    console.log(activeName.value)
    mustSearchData.value = {
      violationUid: showUser.value,
      studyUid: showUser.value
    }
    let data = tableConfig[activeName.value]
    operationApiA.value = data.api
    tableDataRuleA.value = data.table
    if (userThisNmae.value) {
      // let tis = setTimeout(()=>{
      //   clearTimeout(tis)
      tabCombox.value.resetSearch()
      // },1000)
    }
  }, 500)
}

// 表单刷新
const Refresh = () => {
  //用户数据概览
}
// 搜索回调
const searchCallback = (search: any) => {}

// 初始化

//#endregion
</script>
<template>
  <div class="homeboxs">
    <!-- 新增/修改 -->
    <el-dialog v-model="dialogVisible" :title="'设置用户'" @close="clearTan" width="30%">
      <div>
        <el-form ref="searchFormRef" :inline="true">
          <el-form-item prop="name" label="选择用户">
            <personnel v-model="tanOBJUser.userid" v-model:userName="tanOBJUser.name"></personnel>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
    <!-- <component :is="isAdmin ? Admin : Editor" /> -->
    <div class="homeboxs_body">
      <el-card class="box-user">
        <template #header>
          <div class="card-header">
            <span>查看用户:（{{ userThisNmae || "--" }}）</span>
            <el-button type="success" plain @click="dialogVisible = true">设置用户</el-button>
          </div>
        </template>
        <div class="">
          <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            :rules="rules"
            label-width="auto"
            class="demo-ruleForm"
            status-icon
          >
            <el-form-item label="用户名" prop="name">
              <div class="">{{ userInfoData.name }}</div>
            </el-form-item>
            <el-form-item label="头像" prop="image">
              <!-- <el-input v-model="ruleForm.name" /> -->
              <img :src="userInfoData.image" alt="" class="image_head" v-if="userInfoData.image" />
              <div v-else>暂无头像</div>
            </el-form-item>
            <el-form-item label="用户登陆账号" prop="name">
              <div class="">{{ userInfoData.userName }}</div>
            </el-form-item>
            <el-form-item label="角色名字" prop="name">
              <div class="">{{ userInfoData.roleName }}</div>
            </el-form-item>
            <el-form-item label="用户性别" prop="name">
              <div class="">{{ { "1": "男", "2": "女", "3": "未知" }[userInfoData.gender] }}</div>
            </el-form-item>
            <el-form-item label="状态分数" prop="name">
              <div class="">{{ userInfoData.score || 0 }}分</div>
            </el-form-item>
            <el-form-item label="分数状态" prop="name">
              <div class="">{{ { "1": "正常", "2": "异常", "3": "未知" }[userInfoData.scoreState] }}</div>
            </el-form-item>
            <el-form-item label="集团" prop="name">
              <div class="">{{ userInfoData.groupName }}</div>
            </el-form-item>
            <el-form-item label="部门" prop="name">
              <div class="">{{ userInfoData.department }}</div>
            </el-form-item>
            <el-form-item label="公司" prop="name">
              <div class="">{{ userInfoData.groupName }}</div>
            </el-form-item>
            <el-form-item label="分子公司" prop="name">
              <div class="">{{ userInfoData.numeratorCompanies }}</div>
            </el-form-item>
            <el-form-item label="项目" prop="name">
              <div class="">{{ userInfoData.project }}</div>
            </el-form-item>
            <el-form-item label="班组" prop="name">
              <div class="">{{ userInfoData.teams }}</div>
            </el-form-item>
            <el-form-item label="用户状态" prop="name">
              <div class="">{{ userInfoData.name ? (userInfoData.status == 1 ? "启用" : "关闭") : "" }}</div>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <el-card class="box-card cardbox">
        <template #header>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="违规处罚记录" name="first"></el-tab-pane>
            <el-tab-pane label="学习培训记录" name="second"></el-tab-pane>
          </el-tabs>
        </template>
        <div>
          <tabCom
            @Refresh="Refresh"
            @search-callback="searchCallback"
            :title="''"
            :operation-api="operationApiA"
            :table-data-rule="tableDataRuleA"
            :search-data-list="searchDataListJy"
            :needPagination="true"
            :update_show_key="'id'"
            :update_show_value="[0]"
            :formDataWidth="'30%'"
            :isShowDownLoadFile="false"
            :isAddAjaxData="true"
            :mustSearchData="mustSearchData"
            :addText="'举报'"
            :isAddArr="false"
            :updateShow="false"
            :deleteShow="false"
            :isShowAddBtn="false"
            :showSearch="false"
            :showOperate="false"
            ref="tabCombox"
          >
          </tabCom>
        </div>
      </el-card>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.homeboxs {
  // background: #fff;
  height: 100%;
  overflow-y: auto;
  padding: 20px;
  box-sizing: border-box;
  &_box {
    margin-bottom: 20px;
  }
  &_body {
    display: flex;
    justify-content: space-between;
    height: 95%;
    .box-user {
      width: 300px;
      margin-right: 20px;
      overflow-y: auto;
    }
    .box-card {
      flex: 1;
      overflow-y: auto;
    }
  }
}
.notify-history {
  text-align: center;
  padding-top: 12px;
  border-top: 1px solid var(--el-border-color);
}
.image_head {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
}
.cardbox {
  ::v-deep(.el-card__header) {
    border: 0;
  }
}
</style>
